package com.haibei.view.web;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;

import org.apache.http.cookie.Cookie;

import com.common.uitl.Tool;

import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;


public class CookiesManager {

	public static String DOMAIN = Constant.DomainConstant.HTTP_URL;

	public static void saveCookies(Context context, List<Cookie> cookies) {
		String v5_DOMAIN = "wuyou.cn";
		if (cookies != null && cookies.size() > 0) {
			CookieManager cookieManager = CookieManager.getInstance();
			saveCookie(context, DOMAIN, cookies, cookieManager);
			saveCookie(context, v5_DOMAIN, cookies, cookieManager);
			String cookie = cookieManager.getCookie(DOMAIN);
			String test_cookie = cookieManager.getCookie(v5_DOMAIN);
			// 判断是否保存成功
			if (cookie == null || !cookie.contains("PPU")) {
				saveCookie(context, DOMAIN, cookies, cookieManager);
			}
			if (test_cookie == null || !test_cookie.contains("PPU")) {
				saveCookie(context, v5_DOMAIN, cookies, cookieManager);
			}
		}
	}

	private static void saveCookie(Context context, String DOMAIN, List<Cookie> cookies, CookieManager cookieManager) {
		String expiresTime = "Sun, 26-Nov-2013 12:53:07 GMT";
		try {
			CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(context);
			String ppu;
			for (Cookie cookie : cookies) {
				StringBuffer c = new StringBuffer();
				c.append(cookie.getName());
				c.append("=\"");
				c.append(cookie.getValue());
				c.append("\"; DOMAIN=" + DOMAIN);
				c.append(";path=\"/\";expires=\"" + expiresTime + "\"");
				cookieManager.setCookie(DOMAIN, c.toString());

			}
			cookieSyncManager.sync();
		} catch (Exception e) {
			Log.d("debug_login", "异常");
			e.printStackTrace();
		}
	}

	public static void saveAppInfoCookies(Context context) {
		String v5_DOMAIN = "wuyou.cn";
		String mNetType = ""; // 网络类型
		try {
			mNetType = nvl(URLEncoder.encode(nvl(NetUtils.getNetType(context)), "utf-8"));
		} catch (Exception e) {
			Log.d("CookiesManager", "setCookie netType failed & exception is : " + e.getMessage());
			mNetType = "-1";
		}
		String brand = ""; // 手机型号
		try {
			brand = nvl(URLEncoder.encode(Build.BRAND, "utf-8"));
		} catch (Exception e) {
			Log.d("CookiesManager", "setCookie brand failed & exception is : " + e.getMessage());
			brand = "-1";
		}
		String cid = ""; // 城市id

		String cimei = ""; // imei
		try {
			cimei = nvl(URLEncoder.encode(DeviceInfoUtils.getImei(context), "utf-8"));
		} catch (Exception e) {
			Log.d("CookiesManager", "setCookie imei failed & exception is : " + e.getMessage());
			cimei = "-1";
		}

		/*
		 * String productorId = ""; // 产品id try { productorId =
		 * nvl(URLEncoder.encode
		 * (context.getResources().getString(R.string.dumpcatcher_product_id),
		 * "utf-8")); } catch (Exception e) { Log.d("CookiesManager",
		 * "setCookie productorid failed & exception is : " + e.getMessage());
		 * productorId = "-1"; }
		 */
		String ua = "";
		try {
			ua = nvl(URLEncoder.encode(android.os.Build.MODEL, "utf-8"));
		} catch (Exception e) {
			Log.d("CookiesManager", "setCookie ua failed & exception is : " + e.getMessage());
			ua = "-1";
		}
		String osv = ""; // 系统版本号
		try {
			osv = nvl(URLEncoder.encode(android.os.Build.VERSION.RELEASE, "utf-8"));
		} catch (Exception e) {
			Log.d("CookiesManager", "setCookie osv failed & exception is : " + e.getMessage());
			osv = "-1";
		}
		String cversion = ""; // 软件版本号
		try {
			cversion = Tool.getVersionName(context);
		} catch (Exception e) {
			Log.d("CookiesManager", "setCookie cversion failed & exception is : " + e.getMessage());
			cversion = "-1";
		}
		/*
		 * String channelId = ""; // 渠道 try { //
		 * context.getResources().getString(R.string.channelID) channelId =
		 * nvl(URLEncoder.encode(CommonApplication.sChannelId, "utf-8")); }
		 * catch (Exception e) { Log.d("CookiesManager",
		 * "setCookie channel failed & exception is : " + e.getMessage());
		 * channelId = "-1"; }
		 */
		/*
		 * String uid = ""; try { uid =
		 * nvl(URLEncoder.encode(User.getInstance(context).getUserID(),
		 * "utf-8")); } catch (Exception e) { Log.d("CookiesManager",
		 * "setCookie uid failed & exception is : " + e.getMessage()); uid =
		 * "-1"; }
		 */
		String displayPixel = ""; // 分辨率
		try {
			displayPixel = nvl(URLEncoder.encode(DeviceInfoUtils.getDisplay(context), "utf-8"));
		} catch (Exception e) {
			Log.d("CookiesManager", "setCookie pixel failed & exception is : " + e.getMessage());
			displayPixel = "-1";
		}
		String mac = ""; // 手机mac
		try {
			mac = nvl(URLEncoder.encode(DeviceInfoUtils.getMacAddress(context), "utf-8"));
		} catch (Exception e) {
			Log.d("CookiesManager", "setCookie mac failed & exception is : " + e.getMessage());
			mac = "-1";
		}
		String platform = "android";
		String expiresTime = "Sun, 26-Nov-2013 12:53:07 GMT";

	}

	public static void printCookies(Context context, String url) {
		CookieManager cookieManager = CookieManager.getInstance();
		String cookie = cookieManager.getCookie(url);
		Log.i("CookiesManager", "COOKIE=" + cookie);
	}

	public static String createCookieJson(Context context) {
		return DOMAIN;

	}

	public static HashMap<String, String> createHeader(Context context) {
		HashMap<String, String> mMap = new HashMap<String, String>();
		mMap.put("X-Wap-Proxy-Cookie", "none");
		mMap.put("cimei", DeviceInfoUtils.getImei(context));
		return mMap;
	}

	public static void clearCookies(Context context) {
		try {
			CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(context);
			CookieManager cookieManager = CookieManager.getInstance();
			cookieManager.removeAllCookie();
			cookieSyncManager.sync();
			saveAppInfoCookies(context);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String getPPUCookie(Context context) {
		String ppu = "";
		try {
			CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(context);
			CookieManager cookieManager = CookieManager.getInstance();
			String cookies = cookieManager.getCookie(DOMAIN);
			if (!TextUtils.isEmpty(cookies) && cookies.contains("PPU=")) {
				ppu = cookies.substring(cookies.indexOf("PPU=") + 4, cookies.length());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ppu;
	}

	private static String nvl(String param) {
		if (TextUtils.isEmpty(param)) {
			return "";
		} else {
			return param;
		}
	}

}
